<?php


namespace app\index\controller;

use app\common\model\UserAddr;
use app\common\logic\AddrLogic;

class Addr extends Base
{
    /**
     * 展示我的地址
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function myAddr()
    {
        $id = $this->user['id'];
        $info = db('UserAddr')
            ->where('us_id', $id)
            ->where('delete_time', null)
            ->field('addr_detail,id,addr_default,addr_tel,addr_receiver')
            ->order('addr_default desc')
            ->select();
        $this->s_msg(null, $info);
    }

    /**
     * 添加收获地址
     */
    public function addAddr()
    {
        $param = input('post.');
        $validate = validate('Verify');
        if (!$validate->scene('addAddr')->check($param)) {
            $this->e_msg($validate->getError());
        }
        $param['us_id'] = $this->user['id'];
        $param['add_time'] = date('Y-m-d H:i:s');
        //$param['addr_code'] = implode(',', $param['addr_code']);
        $default = model('UserAddr')
            ->where('us_id', $this->user['id'])
            ->where('addr_default', 1)
            ->find();
        if (!$default) {
            $param['addr_default'] = 1;
        }
        $rel = model('UserAddr')->addInfo($param);
        if ($rel) {
            $this->s_msg('添加成功', $rel);
        }
    }

    /**
     * 设置默认地址
     */
    public function setDefaultAddr()
    {
        $map['id'] = input('post.id');
        $param['addr_default'] = 1;
        $addr_m = model('UserAddr');
        $us_id = $addr_m->where($map)->value('us_id');
        $addr_m->where('us_id', $us_id)
            ->where('addr_default', 1)
            ->setDec('addr_default');
        $rel = $addr_m->updateInfo($map, $param);
        if ($rel) {
            $this->s_msg('设置成功');
        } else {
            $this->s_msg('您没有作出修改');
        }
    }

    /**
     * 修改地址
     */
    public function edit()
    {
        $id = input('id');
        if (empty($id)) {
            $this->e_msg('id无效');
        }
        // 查询
        $data = db('user_addr')->where('id', $id)->field('addr_detail,addr_tel,addr_receiver')->find();
        return $this->s_msg('', $data);
    }

    public function editAddr()
    {
        $id = input('id');
        $data = input('post.');
        $validate = validate('Verify');
        if (!$validate->scene('addAddr')->check($data)) {
            $this->e_msg($validate->getError());
        }
        $res = model('User_addr')->save($data, ['id' => $id]);
        if ($res) {
            $this->s_msg('修改成功');
        } else {
            $this->e_msg('修改失败');
        }
    }

    //删除地址 (软删除)
    public function del()
    {
        $id = input('post.id');
        if (!$id) {
            $this->e_msg('非法操作');
        } else {
            $info = model('User_addr')->get($id);
        }
        if (!$info) {
            $this->e_msg('非法操作');
        } else {
            $res = model('User_addr')->destroy($id);
        }
        if ($res) {
            $this->s_msg('删除成功');
        }
    }


}